package org.seaurchin.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.seaurchin.database.*;

import org.seaurchin.category.CategoryTree;

public class CategoryNav extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	    	throws ServletException, IOException {
		
		String category = request.getParameter("selectedCategory");
		if(category == null || category.equals("")) {
			category = "Top";
		}
		
		ServletContext ctx = this.getServletContext();
		CategoryTree catTree = (CategoryTree) ctx.getAttribute("tree");
		
		ArrayList urlList = persist.getSites(category);
		List<String> categoryList = catTree.getChildrenNames(category);
		
		request.setAttribute("currentCategory", category);
		request.setAttribute("urlList", urlList);
		request.setAttribute("categoryList", categoryList);
		
		RequestDispatcher view = request.getRequestDispatcher("categories.jsp");
		
		view.forward(request, response);		
	}
}
